<?php

namespace App\Http\Controllers\Home;

use App\Model\M9_cate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{

    /**
     * 获取主所有主分类id
     */
    static public function getCid($cpid=0)
    {
        $cp= [];
            $cid = M9_cate::where('cpid',$cpid)->get();
            $a = M9_cate::where('cpid','!=',0)->get();

            foreach($cid as $k=>$v){
                $cp[] = $v['cid'];

            }
            return $cp;

    }
    /**
     *
     */
    static public function getScid()
    {
        $cpt=[];
        $a = M9_cate::all();
        foreach($a as $k=>$v){
            $cpt[] = $v['cpid'];
        }
        dd($cpt);
    }

    /**
     * 处理前台数据
     */
    static public function getCatePid($cpid=0)
    {
        $data = DB::table('cate')->where('cpid',$cpid)->get();
        $arr = [];
        foreach($data as $key=>$value)
        {

            $value['sub'] = self::getCatePid($value['cid']);

            $arr[] = $value;
        }
        return $arr;

    }
    static public function getCatePid1($cpid=0)
    {
        $data = DB::table('cate')->where('cpid',$cpid)->get();
        $arr = [];
        $da = [];
        foreach($data as $key=>$value)
        {
            $info = M9_cate::where('cpid',$value['cid'])->get();
            foreach($info as $k=> $v){
                $cgoods = M9_cate::find($v['cid'])->goods()->get();
                $da[] = $cgoods;
                //dd($cgoods);

            }
            $value['goo'] = $da;
            $value['sub'] = self::getCatePid($value['cid']);
            dd($value);
            $arr[] = $value;
        }
        return $arr;

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $qq = self::getCatePid(0);

        $da = [];

        foreach ($qq as $key => $value) {
            $da = [];
            $info = M9_cate::where('cpid', $value['cid'])->get();

            foreach ($info as $k => $v) {
                $cgoods = M9_cate::find($v['cid'])->goods()->get();
                $da[] = $cgoods;

            }
            $qq[$key]['good'] = $da;

        }

      // dd($qq);





            //return view('home.index', ['data' => self::getCatePid(0)]);
            return view('home.index', ['data' => $qq]);
        }




    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
